package com.wtkl.service.impl;

import com.wtkl.mapper.StudentMapper;
import com.wtkl.pojo.*;
import com.wtkl.service.dormitoryService;
import com.wtkl.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;


public class dormitoryServiceImpl implements dormitoryService {

    SqlSessionFactoryUtil sqlSessionFactoryUtil=new SqlSessionFactoryUtil();

    /**
     * 负责人：真正的man
     * 功能：登录
     *1.先判断是学生，宿管还是管理员
     *2.查询账号，账号存在接着查密码是否正确
     *3.密码正确则返回1登录成功，错则返回0密码错误，返回-1则是账号不存在
     *参数：account，password，role
     *返回值：-1 0 1
     */
    @Override
    public Integer login(String password, String account,Integer role) {

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        user user;
        user=null;

        switch (role){
            case 1://学生
                 user = mapper.login1(account,password);
                 break;
            case 2://宿管
                 user = mapper.login2(account,password);
                 break;
            case 3://管理员
                user = mapper.login3(account,password);
                break;

        }

        sqlSession.close();

        if(user==null)
        {
            return 0;   //登录失败
        }
        else{
            return 1;   //登录成功
        }
    }

    //学生查询基本信息
    public List<selectStudent> stuSelectInformation(String id){
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<selectStudent> student = mapper.stuSelectInformation(id);

        sqlSession.close();
        return student;
    }

    //学生查询请假
    public List<leave> stuSelectLeave(String id){
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<leave> student = mapper.stuSelectLeave(id);

        sqlSession.close();
        return student;
    }

    //学生查询通知
    public List<notice> stuSelectNotice(){
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<notice> notices = mapper.stuSelectNotice();
        sqlSession.close();
        return notices;
    }

    //学生添加请假申请
    @Override
    public Integer stuAddLeave(leave leave) {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);


        Integer integer = mapper.stuAddLeave(leave);
        sqlSession.commit();
        sqlSession.close();
        return integer;
    }

    //学生删除请假信息
    public void stuDeleteLeave(int[] ids){
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        mapper.stuDeleteLeave(ids);

        sqlSession.commit();
        sqlSession.close();
    }

    //宿管查询学生信息
    public List<selectStudent>  dorSelectInformation(selectStudentData data)
    {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<selectStudent> selectStudents = mapper.dorSelectInformation(data);



        sqlSession.close();
        return selectStudents;
    }

    //宿管添加通知
    public Integer addNotice(notice notice){
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Integer integer = mapper.addNotice(notice);

        sqlSession.commit();
        sqlSession.close();
        return integer;

    }

    //宿管查询请假条
    public List<leave> selectLeave(leave leave) {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<leave> leave1= mapper.SelectLeave(leave);


        sqlSession.close();
        return leave1;
    }


    public List<dormitoryAdmin> selectDormitoryAdmin(dormitoryAdmin dormitory) {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<dormitoryAdmin> dormitories = mapper.selectDormitoryAdmin(dormitory);


        sqlSession.close();
        return dormitories;
    }

    public List<dormitory> selectDormitory(dormitory dormitory){

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<dormitory> dormitories = mapper.selectDormitory(dormitory);


        sqlSession.close();
        return dormitories;
    }














}

